// vector_test_2_10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<vector>
#include<string>
using namespace std;

//返回只出现一次的数字，其余数字接出现两次 leetcode 136
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int x = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            x ^= nums[i];
        }
        return x;
    }
};

//杨辉三角 leet118
class Solution2 {
public:
    vector<vector<int>> generate(int numRows) {
        int i = 0;
        vector<vector<int>> a;
        a.resize(numRows);
        for (size_t i = 0; i < numRows; ++i)
        {
            //需要对每一个vector开辟空间
            a[i].resize((i + 1), 0);
            a[i][0] = a[i][a[i].size() - 1] = 1;
        }

        for (size_t i = 0; i < numRows; ++i)
        {
            for (size_t j = 0; j < a[i].size(); ++j)
            {
                if (a[i][j] == 0)
                {
                    a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
                }

            }
        }
        return a;

    }
};

//删除相同的元素 只剩k个并排列在vector前面，后面元素删除
class Solution3 {
public:
    int removeDuplicates(vector<int>& nums) {
        size_t j = 0;
        for (size_t i = 1; i < nums.size();)
        {
            if (nums[j] == nums[i])
                ++i;
            else
            {
                nums[++j] = nums[i++];
            }

        }
        nums.erase(nums.begin() + j, nums.end() - 1);
        j++;
        return j;
    }
};
void test_vector()
{
    vector<int> v;
    v.push_back(1); 
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    //支持迭代器
    vector<int>::iterator vb = v.begin();
    while (vb < v.end())
    {
        cout << *vb << " ";
        vb++;
    }
    cout << endl;
    //支持反向迭代器
    vector<int> v2 = v;  //拷贝构造函数
    vector<int>::reverse_iterator  rvb = v2.rbegin();
    while (rvb < v2.rend())
    {
        cout << *rvb << " ";
        rvb++;
    }
    cout << endl;
    //为啥按照1.5倍或者2倍扩容---》既不会造成空间的过多浪费，还能减少扩容次数，比较合理
    cout << "size:" << v2.size() << "capacity:" << v2.capacity() << endl;//空间不够按照1.5倍扩容
    v2.push_back(1);
    cout << "size:" << v2.size() << "capacity:" << v2.capacity() << endl;
    v2.push_back(2);
    cout << "size:" << v2.size() << "capacity:" << v2.capacity() << endl;
    v2.push_back(3);
    cout << "size:" << v2.size() << "capacity:" << v2.capacity() << endl;
    v2.push_back(4);
    cout << "size:" << v2.size() << "capacity:" << v2.capacity() << endl;

    cout << v2.front() << endl;  //首元素
    cout << v2.back() << endl;   //尾部元素


}
void test_vector1()
{
    string s1;       //这个末尾是带有\0的，以字符串的形式展现
    vector<string> v1; //这个末尾是不带\0的，一个一个的字符展现

}

void test_vector2()
{

    vector<vector<int>>  vv;
    vv.resize(10);
    for (size_t i=0;i<vv.size()-1;++i)
    {
        vv[i].resize(10, 0);
    }
    for (size_t i = 0; i < vv.size() - 1; ++i)
    {
        for (size_t j = 0; j < vv[i].size() - 1; ++j)
        {
            cout << vv[i][j] << " ";
       }
        cout << endl;
    }
}


int removeDuplicates(vector<int>& nums) {
    //前后指针
    size_t j = 0;
    for (size_t i = 1; i < nums.size();)
    {
        if (nums[j] == nums[i])
            ++i;
        else
        {
            ++j;
            nums[j] = nums[i];
            ++i;

        }

    }
    nums.erase(nums.begin() + j);
    return j;
}

void test_vector3()
{
    //std::vector<int> myvector;

    //// set some values (from 1 to 10)
    //for (int i = 1; i <= 10; i++) 
    //    myvector.push_back(i);
    //myvector.erase(myvector.begin()+5);
    //for (int i = 0; i <= 4; i++)
    //{
    //    cout << myvector[i] << " ";
    //}

    vector<int> v;
    v.push_back(0);
    v.push_back(0);
    v.push_back(1);
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);
    v.push_back(3);
    v.push_back(4);
    int i = removeDuplicates(v);


}

int main()
{

    test_vector3();

    return 0;
}

